package day9;

class Cup extends Thread {

    // 票数
    static int cup = 100;

    public Cup (String name) {
        super(name);
    }

    @Override
    public void run() {
        while (true) {

            synchronized (Cup.class) {
                if (cup > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(this.getName() + "正在出售限量版杯子:" + (cup--) + "个");
                } else {
                    break;
                }
            }
        }
    }
}

class Cup2{

    // 票数
    static int cup = 100;

    public void chushou() {
        while (true) {

            synchronized (Cup.class) {
                if (cup > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售限量版杯子:" + (cup--) + "个");
                } else {
                    break;
                }
            }
        }
    }
}

public class Demo6 {

    public static void main(String[] args) {
//        Cup s1 = new Cup("官网");
//        Cup s2 = new Cup("实体店");
//
//        s1.start();
//        s2.start();

        final Cup2 s = new Cup2();
        new Thread("官网") {
            @Override
            public void run() {
                s.chushou();
            }
        }.start();
        new Thread("实体店") {
            @Override
            public void run() {
                s.chushou();
            }
        }.start();
    }
}
